package cn.com.shopec.erp.stock.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.purchase.entity.StockPurchase;
import cn.com.shopec.erp.purchase.service.IStockPurchaseService;
import cn.com.shopec.erp.stock.entity.StockOut;
import cn.com.shopec.erp.stock.entity.StockOutPurInfo;
import cn.com.shopec.erp.stock.service.IStockOutPurInfoService;
import cn.com.shopec.erp.stock.vo.StockOutProductVo;
import cn.com.shopec.erp.stock.vo.StockOutPurInfoVo;
import net.sf.json.JSONArray;

@Controller
@RequestMapping("stockmanage/StockOutPurInfo")
public class StockOutPurInfoController {

	@Resource
	private IStockOutPurInfoService stockOutPurInfoService;

	@Resource
	private IStockPurchaseService stockPurchaseService;

	/** 日志. */
	private Logger log = Logger.getLogger(StockOutPurInfoController.class);

	
	/**
	 * 跳转到查询页面
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("toQueryStockOutPurInfo")
	public String toQueryStockOutPurInfo(StockOutPurInfoVo stockOutPurInfo,ModelMap map) throws Exception {
		map.put("stockOutProductId", stockOutPurInfo.getStockOutProductId());
		map.put("prodNo", stockOutPurInfo.getProdNo());
		map.put("stockOutTypeNo", stockOutPurInfo.getStockOutTypeNo());
		return "/stock/stockOut_pur_detail";
	}
	
	/**
	 * 分页查询出库商品采购单信息
	 * 
	 * @param packageInfo
	 * @param pageQuery
	 * @param map
	 * @return
	 */
	@RequestMapping("ajaxPageFinderStockOutPurInfo")
	@ResponseBody
	public String ajaxPageFinderStockOutPurInfo(StockOutPurInfoVo stockOutPurInfo, PageQuery pageQuery, ModelMap map) {
		JSONObject jsonObj = new JSONObject();
		PageFinder<StockOutPurInfoVo> pageFinder = stockOutPurInfoService.queryStockOutPurInfoList(stockOutPurInfo,
				pageQuery);
		jsonObj.put("Rows", pageFinder.getData());
		jsonObj.put("Total", pageFinder.getRowCount());
		return jsonObj.toJSONString();
	}


	/**
	 * 跳转到查询采购单库存关系表页面
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("toQueruyStockPur")
	public String toQueruyStockPur(String prodNo,String whNo,ModelMap map) throws Exception {
		map.put("prodNo", prodNo);
		map.put("whNo", whNo);
		return "/stock/stockOut_pur_select";
	}
	
	/**
	 * 分页查询采购单库存关系表信息
	 * 
	 * @param packageInfo
	 * @param pageQuery
	 * @param map
	 * @return
	 */
	@RequestMapping("ajaxPageFinderStockPur")
	@ResponseBody
	public String ajaxPageFinderStockPur(StockPurchase stockPurchase,PageQuery pageQuery,ModelMap map) {
		JSONObject jsonObj = new JSONObject();
		PageFinder<StockPurchase> pageFinder = stockPurchaseService.queryStockPurchaseForQualified(stockPurchase,
				pageQuery);
		jsonObj.put("Rows", pageFinder.getData());
		jsonObj.put("Total", pageFinder.getRowCount());
		return jsonObj.toJSONString();
	}
	
	/**
	 * 跳转到新增页面
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("toAddStockOutPurInfo")
	public String toAddStockOutPurInfo(StockOutProductVo stockOutProduct,StockOut stockOut, String prentGridIndex,String rowindex,ModelMap map) throws Exception {
		map.put("stockOut", stockOut);
		map.put("stockOutProduct", stockOutProduct);
		map.put("prentGridIndex", prentGridIndex);
		map.put("rowindex", rowindex);
		return "/stock/stockOut_pur_add";
	}
	
	/**
	 * 跳转到新增页面
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("addStockOutPurInfo")
	@ResponseBody
	public String addStockOutPurInfo(String stockOutPurInfo,ModelMap map) throws Exception {
		try {
			JSONArray jsonArray = JSONArray.fromObject(stockOutPurInfo);
			@SuppressWarnings("unchecked")
			List<StockOutPurInfo> stockOutPurInfos = (List<StockOutPurInfo>) JSONArray.toList(jsonArray,
					StockOutPurInfo.class);
			stockOutPurInfoService.addStockOutPurInfo(stockOutPurInfos);
			return SZSCMConstant.SUCCESS;
		} catch (Exception e) {
			log.error("addStockOutPurInfo" + e.getMessage(), e);
		}
		return SZSCMConstant.FAIL;
	}
	

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("deleteStockOutPurInfo")
	@ResponseBody
	public String deleteStockOutPurInfo(StockOutPurInfo stockOutPurInfo) {
		try {
			stockOutPurInfoService.deleteStockOutPurInfo(stockOutPurInfo);
			return SZSCMConstant.SUCCESS;
		} catch (Exception e) {
			log.error("deleteStockOutPurInfo" + e.getMessage(), e);
		}
		return SZSCMConstant.FAIL;
	}
}
